var express = require("express");
var router = express.Router();
var utils = require('../utils/index');
var mysql = require("../connect/index");
var { Users, UsersData } = require('../models/shop/users.model');

router.get("/", function (req, res, next) {
    res.send(utils.setSendResult());
});


// 获取用户信息
router.get("/list", function (req, res, next) {
    const query = req.query;
    Users.findAll().then(result => {
        res.send(utils.setSendResult(200, "查询成功", result));
    }).catch(err => {
        res.send(utils.setSendResult(500, err));
    }).finally(async () => {
    //   await mysql.sequelize.close();
    });
});
// 新增用户信息
router.post("/add", function (req, res, next) {
    const params = req.body;
    Users.create(params).then(result => {
      res.send(utils.setSendResult(200, "添加成功", result));
    }).catch(err => {
        res.send(utils.setSendResult(500, err));
    }).finally(async () => {
      await mysql.sequelize.close();
    });
});
// 批量新增表 - 执行 sql 语句
router.get("/mock/sql", async (req, res, next) => {
    // 假设有两个表的数据
    const shop_users = [
        { id: '01', school: '北京海淀小学',  openid: 'kLddifrood', nickname: '风一样的男孩'},
        { id: '02', school: '北京中关村小学',  openid: 'tcdkdL',nickname: '逗你玩儿'},
        { id: '03', school: '北京皇城根小学',  openid: 'tcEdkdL',nickname: '听说'},
        { id: '04', school: '天津实验小学',  openid: 'tcdDkdL',nickname: '你是个好人'},
        { id: '05', school: '廊坊一小',  openid: 'LcdkdL',nickname: '华尔兹'},
        { id: '06', school: '天津二小',  openid: 'PQkdL',nickname: '等风来'}
        // 更多行...
    ];
    const shop_users_data = [{
      school: '北京海淀小学', openid:'tcdkdL',address: '北京市海淀区', contract:'赵海', phone:'15123332345',userid:'01'
    }, {
        school: '北京海淀小学', openid:'kLddifrood',address: '北京市朝阳区', contract:'王小花', phone:'15223332345',userid:'02'
    }, {
        school: '北京海淀小学', openid:'tcEdkdL',address: '北京市海淀区', contract:'黄晓明', phone:'15323332345',userid:'03'
    }, {
         school: '北京海淀小学', openid:'LcdkdL',address: '北京市海淀区', contract:'卢思宇', phone:'13423332345',userid:'04'
    }, {
        school: '北京海淀小学', openid:'tcdDkdL',address: '北京市海淀区', contract:'李鹏程', phone:'13823332345',userid:'05'
    }, {
         school: '北京海淀小学', openid:'PQkdL',address: '北京市海淀区', contract:'吴迪', phone:'15123332345',userid:'06'
    }]

    try {
        await mysql.sequelize.sync(); // 同步模型到数据库
        // 批量插入数据1
        const insertUsers = await Users.bulkCreate(shop_users);
        // 批量插入数据2
        const insertUsersData = await UsersData.bulkCreate(shop_users_data);
        res.send(utils.setSendResult(200, "批量添加成功", insertUsers, insertUsersData));
    } catch (error) {
        res.send(utils.setSendResult(500,  error));
    } finally {
        await mysql.sequelize.close();
    }
})
// 修改信息
router.post("/edit", function (req, res, next) {
    const params = req.body;
    if (params['id'] == undefined) return res.send(utils.setSendResult(400, "参数错误", {}));
    Users.update(params).then(result => {
        res.send(utils.setSendResult(200, "修改成功", result));
    }).catch(err => {
        res.send(utils.setSendResult(500, err));
    }).finally(async () => {
      await mysql.sequelize.close();
    });
 
});
// 删除信息
router.get("/delete", function (req, res, next) {
    const query = req.query;
    if (query['id'] == undefined) return res.send(utils.setSendResult(400, "参数错误", {}));
    Users.destroy({
        where: {
            id: query['id']
        }
    }).then(result => {
        res.send(utils.setSendResult(200, "删除成功", result));
    }).catch(err => {
        res.send(utils.setSendResult(500, err));
    }).finally(async () => {
       await mysql.sequelize.close();
    });
});

module.exports = router;
